package com.sxz.sbs.applibrary.component;

import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.os.Build;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.PopupWindow;

import com.sxz.sbs.applibrary.R;

/**
 * 下拉选择条件控件
 */

public class PopupCheckBox extends android.support.v7.widget.AppCompatCheckBox implements PopupWindow.OnDismissListener{
    private PopupWindow popupWindow;
    private Context context;
    private int screenWidth;
    private int screenHeight;

    public PopupCheckBox(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        initCheck(context);
    }

    public void initCheck(Context context) {
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        screenWidth = wm.getDefaultDisplay().getWidth();
        screenHeight = wm.getDefaultDisplay().getHeight();
        int[] colors =new int[] { getResources().getColor(R.color.gray), getResources().getColor(R.color.app_color)};
        int[][] states =new int[2][];
        states[0] =new int[] { -android.R.attr.state_checked};
        states[1] =new int[] { android.R.attr.state_checked};
        ColorStateList colorStateList = new ColorStateList(states,colors);
        setTextColor(colorStateList);
        this.setCompoundDrawablesWithIntrinsicBounds(null,null,getResources().getDrawable(R.drawable.select_popup_box),null);
        setCompoundDrawablePadding(10);
        setSingleLine();
        setEllipsize(TextUtils.TruncateAt.END);
        setGravity(Gravity.CENTER);
        setPadding(dp2px(8),0,dp2px(8),0);
        setButtonDrawable(null);
    }
    public void setPopupWindow(final View view) {
        set(view,-1);
    }
    public void setPopupView(final View view, final int screenHeight) {
        set(view,screenHeight);
    }

    private void set (final View view, final int tag) {
        this.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (!isChecked) {
                    if (popupWindow != null) {
                        popupWindow.dismiss();
                    }

                } else {
                    if(popupWindow == null) {
                        LinearLayout layout = new LinearLayout(context);
                        LinearLayout.LayoutParams params;
                        if (tag == -1) {
                            params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                        } else {
                            params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, tag);
                        }
                        view.setLayoutParams(params);
                        layout.addView(view);
                        layout.setBackgroundColor(Color.argb(60, 0, 0, 0));
                        popupWindow = new PopupWindow(layout,screenWidth,screenHeight);
                        popupWindow.setFocusable(true);
                        popupWindow.setBackgroundDrawable(new BitmapDrawable());
                        popupWindow.setOutsideTouchable(true);
                        popupWindow.setAnimationStyle(R.style.popup_check_anim);
                        popupWindow.setOnDismissListener(PopupCheckBox.this);
                        layout.setOnClickListener(new OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                hidePopup();
                            }
                        });
                    }
                    if (Build.VERSION.SDK_INT < 24) {
                        popupWindow.showAsDropDown(PopupCheckBox.this);
                    } else {
                        int[] a = new int[2];
                        PopupCheckBox.this.getLocationInWindow(a);
                        popupWindow.showAtLocation(PopupCheckBox.this, Gravity.NO_GRAVITY, 0, buttonView.getHeight()+a[1]);
                        popupWindow.update();
                    }
                }
            }
        });
    }
    public void hidePopup(){
        if(popupWindow != null && popupWindow.isShowing()) {
            popupWindow.dismiss();
        }
    }

    @Override
    public void onDismiss() {
        this.setChecked(false);
    }
    /**
     * dp转px
     *
     * @param dpValue dp值
     * @return px值
     */
    public  int dp2px(final float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }
}
